package com.gaozhen.demo;

import java.util.Random;

public class Test3 {
    public static void main(String[] args) {
        //3、开发验证码
        //定义一个方法，随机生成一个5位的验证码，每位可能是数字、大小写字母
        String code = createCode(5);
        System.out.println(code);
    }
    public static String createCode(int n){
        String code = "";
        Random r = new Random();
        for (int i = 0; i < n; i++) {
            //生成一个随机字符
            int type = r.nextInt(3);  // 0 1 2
            //通过字符再次随机字符
            switch (type){
                case 0:
                    //大写字符 A 65   Z 65+(0+25)
                    char ch = (char) (r.nextInt(26)+65);
                    code += ch;
                    break;
                case 1:
                    //小写字符    a  97   z   97+(0+25)
                    char ch1 = (char) (r.nextInt(26)+97);
                    code +=ch1;
                    break;
                case 2:
                    //数字
                    code += r.nextInt(10);  //0-9
                    break;
            }
        }
        return code;
    }
}
